﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GroupBy
{
    class GroupBy
    {
        static void Main(string[] args)
        {
            var students = new[]{
                new{ name="Ivan Kolev", groupName="Software engineering"},
                new{name="Anton Stavrev", groupName="Information technology"},
                new{name="Antonia Sybeva", groupName="Computer Science"},
                new{name="Teodor Tenev", groupName="Software engineering"},
                new{name="Toni Gerganov", groupName="Computer Science"},
                new{name="Rali Minkova", groupName="Information technology"}
            };

            var studentsByGroup = from s in students
                                  group s by s.groupName;


            foreach (var group in studentsByGroup)
            {
                int counter = 0;
                Console.Write("Group {0}: ", group.Key);
                foreach (var name in group)
                {
                    if (counter < group.Count() - 1)
                    {
                        Console.Write("{0} ,", name.name);
                        counter++;
                    }
                    else
                    {
                        Console.Write("{0} ", name.name);
                    }

                }
                Console.WriteLine();
            }

            Console.WriteLine("-----------------------------------------------------------");
            #region Extension methods
            //Extension methods
            var studentsGroup = students.GroupBy(t => t.groupName);

            foreach (var group in studentsGroup)
            {
                int counter = 0;
                Console.Write("Group {0}: ", group.Key);
                foreach (var name in group)
                {
                    if (counter<group.Count()-1)
                    {
                        Console.Write("{0} ,", name.name);
                        counter++;
                    }
                    else
                    {
                        Console.Write("{0} ", name.name);
                    }
                    
                }
                Console.WriteLine();
            }
            #endregion

        }
    }
}
